const fs = require('fs');
const path = require('path');

/*
1 3 m2 n2 t2 n2 t3 2 m1 close(可能比t1先执行也可能比其后执行) t1
*/
setTimeout(() => {
  console.log(1);
  const rs = fs.createReadStream(path.join(__dirname, './test.js'));
  rs.on('close', () => console.log('close'));
  rs.on('data', () => {
    rs.destroy(); /** 可读流的destroy会触发 close事件, close事件在6阶循环中最后一阶,但需要注意的是IO操作是比较慢的，So它的回调可能比t1先执行,也可能比其后执行*/
    setImmediate(() => console.log('m1'));
    setTimeout(() => console.log('t1'));
    console.log(2);
  });

  /** ↓因为这里是嵌套在setTimout里的,故setImmediate一定比setTimeout先执行*/
  setImmediate(()=>{
    console.log('m2');
    process.nextTick(() => {console.log('n2');});
  });

  console.log(3);
  setTimeout(() => {
    console.log('t2');
    process.nextTick(() => console.log('n2'));
  });
  setTimeout(()=>{
    console.log('t3');
  })
});
